import { Component, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { CommonService } from 'src/app/service/common/common.service';
import { LoginService } from 'src/app/service/login.service';
import { MessageService } from 'src/app/service/message.service';
import { formValid } from 'src/app/utils/form-valid';
import { PASS_REG, PHONE_REG } from 'src/app/utils/regExp';
import { jsePwd } from 'src/app/utils/utils';

@Component({
  selector: 'app-reset-pwd',
  templateUrl: './reset-pwd.component.html',
  styleUrls: ['./reset-pwd.component.scss']
})
export class ResetPwdComponent implements OnInit, OnChanges {

  constructor(
    private common: CommonService,
    private message: MessageService,
    private login: LoginService
  ) { }

  @Input() nzVisible = false
  @Output() nzVisibleChange = new EventEmitter()
  ngOnInit(): void {
  }

  ngOnChanges(changes: SimpleChanges): void {
    if(changes['nzVisible'].currentValue){
      this.resetPwdForm.reset()
    }
  }

  resetPwdForm: FormGroup = new FormGroup({
    phone: new FormControl('', [Validators.required, Validators.pattern(PHONE_REG)]),
    password: new FormControl('', [Validators.required, Validators.pattern(PASS_REG)]),
    confirmPassword: new FormControl('', [Validators.required]),
    vCode: new FormControl('', [Validators.required])
  })

  passwordsDiffer = false
  passwordChange(pass: string){
    if(this.password?.value !== this.confirmPassword?.value){
      this.passwordsDiffer = true
    }else{
      this.passwordsDiffer = false
    }
  }

  nzOnCancel(){
    this.nzVisible = false
    this.nzVisibleChange.emit(this.nzVisible)
  }

  nzLoading = false
  nzOnOk(){
    if((!formValid(this.resetPwdForm)) || this.passwordsDiffer){
      return
    }
    this.nzLoading = true
    this.common._getPublicKey().subscribe(res => {
      if(res){
        const publicKey = res['publicKey']
        const data = { password: jsePwd(publicKey, this.password?.value) }
        this.login._loginByVCode(this.phone?.value, this.vCode?.value).subscribe({
          next: res => {
            console.log(res)
            if(res.authentication === 'success'){
              this.login._initPassword(data).subscribe({
                next: res => {
                  if(res){
                    // 修改成功
                    this.message.success(res.message)
                    this.nzVisible = false
                    this.nzVisibleChange.emit(this.nzVisible)
                  }
                },
                error: error => {
                  this.message.createErrorMessage(error)
                }
              })
            }else if(res.error && res.error.message){
              this.message.error(res.error.message)
            }else{
              this.message.error('认证失败, 请重试')
            }
            this.nzLoading = false
          },
          error: error => {
            this.nzLoading = false
            this.message.createErrorMessage(error)
          }
        })
        return
      }
      this.message.error('重置密码失败, 请重试')
      this.nzLoading = false
    }, error => {
      this.message.createErrorMessage(error)
      this.nzLoading = false
    })
  }

  //#region get set
  get phone(){
    return this.resetPwdForm.get('phone')
  }
  get password(){
    return this.resetPwdForm.get('password')
  }
  get confirmPassword(){
    return this.resetPwdForm.get('confirmPassword')
  }
  get vCode(){
    return this.resetPwdForm.get('vCode')
  }
  //#endregion

}
